package 笔试;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class 二叉树的中序遍历 {
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        if(root == null) {
            return res;
        }
        Stack<TreeNode> st = new Stack<>();
        TreeNode cur = root;
        while(!st.isEmpty() || cur != null) {
            while(cur != null) {
                st.push(cur);
                cur = cur.left;
            }
            TreeNode tmp = st.pop();
            res.add(tmp.val);
            if(tmp.right != null) {
                cur = tmp.right;
            }
        }
        return res;
    }
}
